(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.rptEditController', rptEditController);
	rptEditController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'ajaxServiceFactory'];
	function rptEditController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, ajaxServiceFactory) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("companyRptEditPopup"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getCompanyRptOptions : {
				url : 'do/cda/dataList/companyRptOptions',
				method : 'get'
			},
		});
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.companyRpt = {};
		ctrl.formName = "companyRptForm";
		
		ctrl.init = function() {
			ctrl.reportParameterGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.reportParmsCfg.tableConfigs.table, paramsRowSelectionChanged);
			ctrl.reportParameterGridOptions.data = 'vm.companyRpt.reportParms';
			ctrl.availableFormatsOptions = getReportFormatsOptions();
		
			ajaxService.getCompanyRptOptions().then(function(response) {
				return $q(function(resolved, rejected) {
					var companyRptOptions = {};
					if (response.data) {
						angular.forEach(response.data, function(value, key) {
							var tempValue = {};
							tempValue.data = value;
							companyRptOptions[key] = tempValue;
						});
					}
					ctrl.yesnoOptions = companyRptOptions.yesnoOptions;
					ctrl.archiveMethodOptions = companyRptOptions.archiveMethodOptions;
					ctrl.localeCodeOptions = companyRptOptions.localeCodeOptions;
					resolved();
				});
			}, angular.noop).then(function(){
				if (ctrl.dialogsModule.params.companyRpt && ctrl.dialogsModule.params.companyRpt.reportCode) {
					
					authService.eidtRpt(ctrl.dialogsModule.params.companyRpt).then(function(response) {
						//
						if (ctrl.dialogsModule.params.companyRpt) {
							ctrl.companyRpt = angular.copy(ctrl.dialogsModule.params.companyRpt) || {};
							ctrl.companyRpt.reportParms = ctrl.companyRpt.reportParms || [];
//						ctrl.companyRpt.report = {};
							ctrl.companyRpt.reportFormatsList = ctrl.companyRpt.reportFormatsList ||[];
							if ( ctrl.companyRpt.dataStatus == "C" && ctrl.companyRpt.reportFormatsList.length == 0) {
								var formats = ctrl.companyRpt.reportFormats.split(",");
								for(var i = 0; i < formats.length; i++){
									if(formats[i] != ""){
										ctrl.companyRpt.reportFormatsList.push(formats[i]);
									}
								}
							}
						} else {
							ctrl.companyRpt = response.data || {};
							ctrl.companyRpt.reportParms = ctrl.companyRpt.reportParms || [];
						}
					}, angular.noop);
				}
			});
		};
		
		$scope.$on("REPORT.REPORTNAME_OK", function(event, param) {
			var returnEntities = param.returnEntities;
			if (!returnEntities || returnEntities.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return false;
			}
			ctrl.companyRpt.report = returnEntities[0];
			ctrl.companyRpt.reportCode = returnEntities[0].reportCode;
			ctrl.companyRpt.isEnabled = returnEntities[0].isEnabled;
			ctrl.companyRpt.archiveMethod = returnEntities[0].archiveMethod;
			ctrl.companyRpt.archivePeriod = returnEntities[0].archivePeriod;
			ctrl.companyRpt.localeCode = returnEntities[0].localeCodes;
			ctrl.companyRpt.reportFormatsList = returnEntities[0].reportFormatList;
			ctrl.companyRpt.reportParms = returnEntities[0].reportParms;
			//
			event.preventDefault();
			event.stopPropagation();
		});
		
		$scope.$on("REPORT.REPORTNAME_CANCEL", function(event, param) {
			event.preventDefault();
			event.stopPropagation();
		});
		
		function paramsRowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedParameters = selectedRows;
		}
		
		ctrl.addReportParameter = function() {
			ctrl.companyRpt.reportParms.push({
			});
		};

		function getReportFormatsOptions() {
			return {
				"data" : [ {
					"label" : "PDF",
					"value" : "PDF"
				},{
					"label" : "XLS",
					"value" : "XLS"
				},{
					"label" : "CSV",
					"value" : "CSV"
				},{
					"label" : "HTML",
					"value" : "HTML"
				}]
			};
		}
		
		ctrl.ok = function() {
			if(ctrl.companyRpt.reportParms && ctrl.companyRpt.reportParms.length > 0){
				   for (var i = 0; i < ctrl.companyRpt.reportParms.length; i++) {
				    	var reportParm = ctrl.companyRpt.reportParms[i];
				    	if(!reportParm.name && !reportParm.value){
					    	modalService.openErrorDialogs({
					    		messages : ['Name is required.','Value is required.']
							});
					    	return;
					  }else if(!reportParm.name){
					    	modalService.openErrorDialogs({
								messages : [ 'Name is required.' ]
							});
					    	return;
					  }else if(!reportParm.value){
					    	modalService.openErrorDialogs({
								messages : [ 'Value is required.' ]
							});
					    	return;
					  }
				    }
			}
			
			if ($scope.companyRptForm) {
				$scope.companyRptForm.$setSubmitted();
				if(!$scope.companyRptForm.$valid) {
					dialogsModule.showErrorMessages($scope.companyRptForm);
					return;
				}
			}
			if ((!$scope.companyRptForm || $scope.companyRptForm.$valid) && (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.companyRpt) !== false)) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);